package gr.unipi.interfaces;

import gr.unipi.cinema.Movie;
import gr.unipi.cinema.MovieData;

import javax.ws.rs.*;
import javax.ws.rs.core.*;

@Path("/movie")
public class MovieResource {
	
	// The @Context annotation allows us to have certain contextual objects
    // injected into this class.
    // UriInfo object allows us to get URI information (no kidding).
    @Context
    UriInfo uriInfo;
 
    // Another "injected" object. This allows us to use the information that's
    // part of any incoming request.
    // We could, for example, get header information, or the requestor's address.
    @Context
    Request request;
    
    @GET
    @Path("list")
    @Produces("application/json; charset=UTF-8")
    public MovieData[] getMovieList(@QueryParam("id") String idStr, @QueryParam("qstr") String qStr, @QueryParam("state") String qState) {
    	int id = 0;
        if ((idStr != null) && (idStr.length() > 0)) {
        	try {
        		id = Integer.parseInt(idStr);
        	}
        	catch (Exception ex) {
        		return new MovieData[0];
        	}
        }
        
        int state = 1;
        if ((qState != null) && (qState.length() > 0)) {
        	try {
        		state = Integer.parseInt(qState);
        	}
        	catch (Exception ex) {
        		return new MovieData[0];
        	}
        }
        return Movie.getMovieData(id, qStr, state);
    }
}
